local guixin = fk.CreateSkill {

  name = "joy__guixin",

  tags = {  },

}



guixin:addEffect(fk.Damaged, {
  name = "joy__guixin",
  anim_type = "masochism",
  events = {fk.Damaged},
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _ = 1, data.damage do
      if self.cancel_cost or not player:hasSkill(guixin.name) or
      table.every(player.room:getOtherPlayers(player), function (p) return p:isAllNude() end) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, guixin.name, data) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, table.map(room.alive_players, Util.IdMapper))
    local choises = {"$Hand","$Equip","$Judge"}
    local choice = room:askForChoice(player,choises,guixin.name,"#joy__guixin-choose")
    local area = table.indexOf(choises, choice)
    local n = 0
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if player.dead then break end
      local cards = p.player_cards[area]
      if #cards == 0 then cards = p:getCardIds("hej") end
      if #cards > 0 then
        n = n + 1
        room:obtainCard(player, table.random(cards), false, fk.ReasonPrey)
      end
    end
    if not player.dead and player.faceup and n > 4 then
      player:turnOver()
    end
  end,
})

return guixin